package com.douyu.water.core.web.controller;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sound.sampled.AudioFormat.Encoding;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang3.StringUtils;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.douyu.water.core.Configure;
import com.douyu.water.core.DwzResult;
import com.douyu.water.core.PagePara;
import com.douyu.water.core.model.Area;
import com.douyu.water.core.model.Data;
import com.douyu.water.core.model.Rtu;
import com.douyu.water.core.model.User;
import com.douyu.water.core.model.WtldRtu;
import com.douyu.water.core.model.WtldSzyRtu;
import com.douyu.water.core.model.ZKRtu;
import com.douyu.water.core.service.DataService;
import com.douyu.water.core.service.EquiService;
import com.douyu.water.util.CreateExcelPoi;
import com.douyu.water.util.JsonUtil;
import com.douyu.water.util.ParseXmlFile;
import com.douyu.water.util.XmlConfig;
import com.douyu.water.util.kelppaneltiming;

@Controller
@RequestMapping("/equi/*")
public class EquiController {

	@Resource
	private EquiService equiService;
	
	kelppaneltiming kel=new kelppaneltiming();
	
	@Resource
	private DataService dataService;
	/**
	 * 检索所有的设备
	 * @param request
	 * @param record
	 * @param page
	 * @return
	 */
	@RequestMapping("/select")
	public String select(HttpServletRequest request,Rtu record,PagePara page)
	{
		String result = "WEB-INF/jsp/equi/list";
		try
		{
			User user = (User)(request.getSession().getAttribute(Configure.LOGIN_USER));
			List<Rtu> list = equiService.select(record, page,user.getUcode());
//			检索地区列表
			List<Area> areaList = equiService.selectArea(user.getUcode().trim());
			request.setAttribute("areaList", areaList);
			request.setAttribute("list", list);
			request.setAttribute("rtu", record);
			request.setAttribute("pagePara", page);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return result;
	}
	/**
	 * 检索DataCmd表
	 * 用于修改设备监测频率
	 * **/
	@RequestMapping("/selectDataCmd")
	public String selectDataCmd(HttpServletRequest request,Rtu record,PagePara page)
	{
		String result = "WEB-INF/jsp/equi/cmd";
		DwzResult dwzResult = new DwzResult();
		request.setAttribute("dwzResult", dwzResult);
		try
		{
			Rtu rtu = equiService.selectByStation(record);
			if(rtu != null)
			{
				request.setAttribute("rtuu", rtu);
				request.getSession().setAttribute("time", rtu.time);
				request.getSession().setAttribute("jiange", rtu.jiange);
				
			}
			else
			{
				dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
				dwzResult.setMessage("未能获取设备信息");
				return DwzResult.URL;
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
			dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
			dwzResult.setMessage("系统错误:" + e.getMessage());
			return DwzResult.URL;
		}
		return result;
	}
	/**
	 * 修改频率
	 * **/
	@RequestMapping("/editDataCmd")
	public String editDataCmd(HttpServletRequest request,Rtu record,PagePara page)
	{
		DwzResult dwzResult = new DwzResult();
		request.setAttribute("dwzResult", dwzResult);
		String date=request.getParameter("time");
		String jiange=request.getParameter("jiange");
		Rtu rtu = equiService.selectByStation(record);
		String data=kel.getdata(date,jiange,date,jiange);
		byte[]ba=kelppaneltiming.hexStringToByte(data.toUpperCase());
		List<Rtu> list=equiService.selectCmdAll();
		List nums=new ArrayList();
		for (int j = 0; j < list.size(); j++) {
			nums.add(list.get(j).getCommandsernum());
			}
		if(rtu!=null){
			int i=0;
			while(i<=1000){
				if(!nums.contains(i)){
					rtu.setCommandsernum(i);
					break;
				}
				i++;
				}
		}
		
		if(rtu!=null){
			rtu.setCommanddata(ba);
		int res=equiService.updateCmd(rtu);
		if(res>0){
			rtu.time=date;
			rtu.jiange=jiange;
			dwzResult.setStatusCode(DwzResult.STATUS_CODE_SUCCESS);
			dwzResult.setMessage("设置成功");
			dwzResult.setCloseCurrent(false);
			dwzResult.setNavTabId("cmd");
		}
		else{
			dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
			dwzResult.setMessage("设置失败");
		}
		}else{
			dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
			dwzResult.setMessage("设置失败");
		}
		return DwzResult.URL;
	}
	/**
	 * 修改设备信息表
	 * equi的view页面
	 * **/
	@RequestMapping("/updateRtu")
	public String updateRtu(HttpServletRequest request,Rtu record)
	{
		DwzResult dwzResult = new DwzResult();
		request.setAttribute("dwzResult", dwzResult);
		record.setSensordepth(record.getSensordepth()*1000);
		record.setHighlevel(record.getHighlevel()*1000);
		record.setGroundhigh(record.getGroundhigh()*1000);
		//System.out.println(record.getStationnumber()+record.getFactory());
		int res=equiService.updateRtu(record);
		if(res>0){
			dwzResult.setStatusCode(DwzResult.STATUS_CODE_SUCCESS);
			dwzResult.setMessage("修改成功");
			dwzResult.setCloseCurrent(false);
			dwzResult.setNavTabId("view");
		}
		else{
			dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
			dwzResult.setMessage("设置失败");
		}
		return DwzResult.URL;
	}
	/**
	 * 检索Rtu表的设备数据
	 * @param request
	 * @param record
	 * @param page
	 * @return
	 */
	@RequestMapping("/selectRtu")
	public String selectRtu(HttpServletRequest request,Rtu record,PagePara page)
	{
		String result = "WEB-INF/jsp/equi/list";
		try
		{
			User user = (User)(request.getSession().getAttribute(Configure.LOGIN_USER));
			List<Rtu> list = equiService.selectRtu(record, page,user.getUcode());
//			检索地区列表
			List<Area> areaList = equiService.selectArea(user.getUcode().trim());
			request.setAttribute("areaList", areaList);
			request.setAttribute("list", list);
			request.setAttribute("rtu", record);
			request.setAttribute("pagePara", page);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 检索tblJING表的设备数据
	 * 中科水质设备
	 * 中科服务器WaterMonitor数据库
	 * @param request
	 * @param record
	 * @param page
	 * @return
	 */
	@RequestMapping("/selectZKRtu")
	public String selectZKRtu(HttpServletRequest request,ZKRtu record,PagePara page)
	{
		String result = "WEB-INF/jsp/shuizhi/zkrtu";
		try
		{
			
			User user = (User)(request.getSession().getAttribute(Configure.LOGIN_USER));
			record.setQuyu("台头镇");
			List<ZKRtu> list = equiService.selectZKRtu(record, page,user.getUcode());
//			检索地区列表
			List<Area> areaList = equiService.selectArea(user.getUcode().trim());
			request.setAttribute("areaList", areaList);
			request.setAttribute("list", list);
			request.setAttribute("rtu", record);
			request.setAttribute("pagePara", page);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return result;
	}
	
	
	/**
	 * 检索t_第四级数据表的设备数据
	 * 大宇水质设备
	 * 大宇服务器旧版水位数据库
	 * @param request
	 * @param record
	 * @param page
	 * @return
	 */
	@RequestMapping("/selectWtldRtu")
	public String selectWtldRtu(HttpServletRequest request,WtldRtu record,PagePara page)
	{
		String result = "WEB-INF/jsp/shuizhi/wtldrtu";
		try
		{
			record.setHaopai("跃通");
			User user = (User)(request.getSession().getAttribute(Configure.LOGIN_USER));
			List<WtldRtu> list = equiService.selectWtldRtu(record, page,user.getUcode());
//			检索地区列表
			List<Area> areaList = equiService.selectArea(user.getUcode().trim());
			request.setAttribute("areaList", areaList);
			request.setAttribute("list", list);
			request.setAttribute("rtu", record);
			request.setAttribute("pagePara", page);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return result;
	}
	/**
	 * 检索设备信息表的设备数据
	 * 大宇七参数水质设备
	 * 大宇服务器WtldSzy数据库
	 * @param request
	 * @param record
	 * @param page
	 * @return
	 */
	@RequestMapping("/selectWtldSzyRtu")
	public String selectWtldSzyRtu(HttpServletRequest request,WtldSzyRtu record,PagePara page)
	{
		String result = "WEB-INF/jsp/shuizhi/wtldszyrtu";
		try
		{
			
			User user = (User)(request.getSession().getAttribute(Configure.LOGIN_USER));
			List<WtldSzyRtu> list = equiService.selectWtldSzyRtu(record, page,user.getUcode());
//			检索地区列表
			List<Area> areaList = equiService.selectArea(user.getUcode().trim());
			request.setAttribute("areaList", areaList);
			request.setAttribute("list", list);
			request.setAttribute("rtu", record);
			request.setAttribute("pagePara", page);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 首页根据所选地区或者名称检索设备
	 * @param request
	 * @param record
	 * @return
	 */
	@RequestMapping("/selectIndex")
	public String selectIndex(HttpServletRequest request,Rtu record)
	{
		String result = "indexMap";
		User login = (User)(request.getSession().getAttribute(Configure.LOGIN_USER));
		List<Rtu> list = equiService.select(record, null,login.getUcode());
//		检索地区列表
		List<Area> areaList = equiService.selectArea(login.getUcode().trim());
		String lng;
		String lat;
		int zoom;
		if(!StringUtils.isBlank(record.getAreaCode()))
		{
			Area area = equiService.selectSingArea(record.getAreaCode());
			lng=area.getLng();
			lat=area.getLat();
			zoom=9;
		}
		else if(areaList != null && areaList.size() > 0)
		{
			lng=areaList.get(0).getLng();
			lat=areaList.get(0).getLat();
			if(areaList.size() == 1)
			{
				zoom=9;
			}
			else
			{
				zoom=7;
			}
		}
		else
		{
			lng="116.98";
			lat="36.67";		
			zoom=7;
		}
		if(record.getStationname()!=null&&record.getStationname()!=""){
			for (int i = 0; i <list.size(); i++) {
				if(!list.get(i).getLongitude().equals("0.0")&&!list.get(i).getLatitude().equals("0.0")){
					lng=list.get(i).getLongitude();
					lat=list.get(i).getLatitude();
			}
			}
		}
		String str = JsonUtil.convertListToJSONArray(list, null).toString();
		
		request.setAttribute("lng", lng);
		request.setAttribute("lat", lat);
		request.setAttribute("zoom", zoom);
		request.setAttribute("data", str);
		request.setAttribute("areaList", areaList);
		request.setAttribute("list", list);
		request.setAttribute("rtus", record);
		
		return result;
	}
	
	/**
	 * 检索Common表中的设备信息
	 * @param request
	 * @param record
	 * @param page
	 * @return
	 */
	@RequestMapping("/selectCommon")
	public String selectCommon(HttpServletRequest request,Rtu record,PagePara page)
	{
		String result = "WEB-INF/jsp/equi/listc";
		try
		{
			System.out.println(record.getStationnumber());
			System.out.println(request.getParameter("stationnumber"));
			User user = (User)(request.getSession().getAttribute(Configure.LOGIN_USER));
			record.setFactory(2);
			List<Rtu> list = equiService.selectCommon(record, page,user.getUcode());
//			检索地区列表
			List<Area> areaList = equiService.selectArea(user.getUcode().trim());
			request.setAttribute("areaList", areaList);
			request.setAttribute("list", list);
			request.setAttribute("rtu", record);
			request.setAttribute("pagePara", page);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return result;
	}
	/**
	 * 查询博兴大宇的设备
	 * **/
	@RequestMapping("/selectBoXing")
	public String selectBoXing(HttpServletRequest request,Rtu record,PagePara page)
	{
		String result = "WEB-INF/jsp/equi/listb";
		try
		{
			record.setFactory(3);
			User user = (User)(request.getSession().getAttribute(Configure.LOGIN_USER));
			List<Rtu> list = equiService.selectBoXing(record, page,user.getUcode());
//			检索地区列表
			List<Area> areaList = equiService.selectArea(user.getUcode().trim());
//			String str = JsonUtil.convertListToJSONArray(list, null).toString();
//			request.setAttribute("data", str);
			request.setAttribute("areaList", areaList);
			request.setAttribute("list", list);
			request.setAttribute("rtu", record);
			request.setAttribute("pagePara", page);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 检索所有设备，返回json格式字符串
	 * @param response
	 * @param request
	 * @param record
	 */
	@RequestMapping("/selectAll")
	public void selectAll(HttpServletResponse response,HttpServletRequest request,Rtu record)
	{
		try
		{
			User user = (User)(request.getSession().getAttribute(Configure.LOGIN_USER));
			List<Rtu> list = equiService.select(record, null,user.getUcode());
			String str = JsonUtil.convertListToJSONArray(list, null).toString();
			response.getWriter().write(str);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	/**
	 * 查看设备信息
	 * @param request
	 * @param record
	 * @return
	 */
	@RequestMapping("/selectSingle")
	public String seelctSingle(HttpServletRequest request,Rtu record)
	{
		String result = "WEB-INF/jsp/equi/view";
		DwzResult dwzResult = new DwzResult();
		request.setAttribute("dwzResult", dwzResult);
		try
		{
			Rtu rtu = equiService.selectByStation(record);
			if(rtu != null)
			{
				request.setAttribute("rtuu", rtu);
			}
			else
			{
				dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
				dwzResult.setMessage("未能获取设备信息");
				return DwzResult.URL;
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
			dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
			dwzResult.setMessage("系统错误:" + e.getMessage());
			return DwzResult.URL;
		}
		return result;
	}
	
	/**
	 * 查看中科水质设备信息
	 * @param request
	 * @param record
	 * @return
	 */
	@RequestMapping("/selectZKSingle")
	public String selectZKSingle(HttpServletRequest request,ZKRtu record)
	{
		String result = "WEB-INF/jsp/shuizhi/zkrtuview";
		DwzResult dwzResult = new DwzResult();
		request.setAttribute("dwzResult", dwzResult);
		try
		{
			List<ZKRtu> rtus = equiService.selectZKRtu(record, null, null);			
			if(rtus != null)
			{
				ZKRtu rtu=rtus.get(0);
				request.setAttribute("rtuu", rtu);
			}
			else
			{
				dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
				dwzResult.setMessage("未能获取设备信息");
				return DwzResult.URL;
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
			dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
			dwzResult.setMessage("系统错误:" + e.getMessage());
			return DwzResult.URL;
		}
		return result;
	}
	
	/**
	 * 查看大宇三参数设备信息
	 * @param request
	 * @param record
	 * @return
	 */
	@RequestMapping("/selectWtldSingle")
	public String selectWtldSingle(HttpServletRequest request,WtldRtu record)
	{
		String result = "WEB-INF/jsp/shuizhi/wtldrtuview";
		DwzResult dwzResult = new DwzResult();
		request.setAttribute("dwzResult", dwzResult);
		try
		{
			List<WtldRtu> rtus = equiService.selectWtldRtu(record, null, null);			
			if(rtus != null)
			{
				WtldRtu rtu=rtus.get(0);
				request.setAttribute("rtuu", rtu);
			}
			else
			{
				dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
				dwzResult.setMessage("未能获取设备信息");
				return DwzResult.URL;
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
			dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
			dwzResult.setMessage("系统错误:" + e.getMessage());
			return DwzResult.URL;
		}
		return result;
	}
	
	/**
	 * 查看大宇七参数设备信息
	 * @param request
	 * @param record
	 * @return
	 */
	@RequestMapping("/selectWtldSzySingle")
	public String selectWtldSzySingle(HttpServletRequest request,WtldSzyRtu record)
	{
		String result = "WEB-INF/jsp/shuizhi/wszyrtuview";
		DwzResult dwzResult = new DwzResult();
		request.setAttribute("dwzResult", dwzResult);
		try
		{
			List<WtldSzyRtu> rtus = equiService.selectWtldSzyRtu(record, null, null);			
			if(rtus != null)
			{
				WtldSzyRtu rtu=rtus.get(0);
				request.setAttribute("rtuu", rtu);
			}
			else
			{
				dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
				dwzResult.setMessage("未能获取设备信息");
				return DwzResult.URL;
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
			dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
			dwzResult.setMessage("系统错误:" + e.getMessage());
			return DwzResult.URL;
		}
		return result;
	}
	
	/**
	 * 根据经纬度查看设备信息
	 * @param request
	 * @param record
	 * @return
	 */
	@RequestMapping("/selectEquip")
	public String selectEquip(HttpServletRequest request,Rtu record)
	{
		String result = "WEB-INF/jsp/equi/view";
		DwzResult dwzResult = new DwzResult();
		request.setAttribute("dwzResult", dwzResult);
		try
		{
			Rtu rtu = equiService.selectByLocation(record);
			if(rtu != null)
			{
				request.setAttribute("rtuu", rtu);
			}
			else
			{
				dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
				dwzResult.setMessage("未能获取设备信息");
				return DwzResult.URL;
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
			dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
			dwzResult.setMessage("系统错误:" + e.getMessage());
			return DwzResult.URL;
		}
		return result;
	}
	@RequestMapping("/insert")
	public String insert(HttpServletRequest request,Rtu record)
	{
		return"WEB-INF/jsp/equi/insert";
	}
	/**
	 * 增加正元设备
	 * **/
	@RequestMapping("/insertRtu")
	public String insertRtu(HttpServletRequest request,Rtu record)
	{
		Date setup=equiService.getdate(request.getParameter("setup"));
		record.setSetuptime(setup);
		DwzResult dwzResult = new DwzResult();
		request.setAttribute("dwzResult", dwzResult);
		int res=equiService.insert(record);
		if(res>0){
			dwzResult.setStatusCode(DwzResult.STATUS_CODE_SUCCESS);
			dwzResult.setMessage("添加成功");
			dwzResult.setCloseCurrent(false);
			dwzResult.setNavTabId("view");
		}
		else{
			dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
			dwzResult.setMessage("添加失败");
		}
		return DwzResult.URL;
	}
	/**
	 * 删除正元设备
	 * **/
	@RequestMapping("/deleteRtu")
	public String deleteRtu(HttpServletRequest request,String stationnumber)
	{
		String result = "WEB-INF/jsp/equi/list";
		int res=equiService.deleteRtu(stationnumber);
		//int res=1;
		if(res>0){
			request.setAttribute("mess", res);
		}
		selectRtu(request, null, null);
		return result;
	}
	
	/**
	 * 查看设备数据曲线
	 * @param request
	 * @param record
	 * @return
	 */
	@RequestMapping("/selectEquipLine")
	public String selectEquipLine(HttpServletRequest request,Rtu record)
	{
		String result = "WEB-INF/jsp/data/view";
		DwzResult dwzResult = new DwzResult();
		request.setAttribute("dwzResult", dwzResult);
		try
		{
			Rtu rtu = equiService.selectByLocation(record);
			
			if(rtu != null)
			{
				Data data = new Data();
				data.setStationnumber(rtu.getStationnumber());
				Map<String,String> list = dataService.selectLine(data,rtu.getFactory());			
				request.setAttribute("datal", list);
				request.setAttribute("rtuu", rtu);
			}
			else
			{
				dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
				dwzResult.setMessage("未能获取设备信息");
				return DwzResult.URL;
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
			dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
			dwzResult.setMessage("系统错误:" + e.getMessage());
			return DwzResult.URL;
		}
		return result;
	}
	@RequestMapping("/selectEquipAjax")
	@ResponseBody()
	public String selectEquipAjax(HttpServletRequest request,Rtu record,PagePara page,HttpServletResponse response)
	{
		response.setContentType("text/html;charset=utf-8");

		try {
			request.setCharacterEncoding("utf-8");
			
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		DwzResult dwzResult = new DwzResult();
		request.setAttribute("dwzResult", dwzResult);
		Rtu rtu=equiService.selectByLocation(record);
		Data data = new Data();
		if(rtu.getStationnumber()!=null&&rtu.getStationnumber()!=""){
		data.setStationnumber(rtu.getStationnumber());
		}else{
			data.setStationnumber("无匹配名称");	
		}
		List<Data> list = dataService.select(data, page,0);
		String data1=JSONObject.fromObject(list.get(0)).toString();
		return data1;
	}
	
	/**
	 * 导出设备列表
	 * @param response
	 * @param request
	 * @param record
	 */
	@RequestMapping("/selectExport")
	public void selectExport(HttpServletResponse response,HttpServletRequest request,Rtu record,ZKRtu zkrtu,WtldRtu wtld,WtldSzyRtu wtldszy)
	{
		try
		{
			User user = (User)(request.getSession().getAttribute(Configure.LOGIN_USER));
			
			String flag = (String)(request.getParameter("flag"));
			List list=null;
			String path = request.getRealPath("/");
			path = path.replaceAll("[\\\\]+","/");
			String xmlFile =null; 
			
			if(flag != null && flag.trim().equals("1"))
			{
				xmlFile=path + "config/export_equi.xml";
				record.setFactory(null);
				list=new ArrayList<Rtu>();
				list = equiService.selectRtu(record, null,user.getUcode());
			}
			else if(flag != null && flag.trim().equals("2"))
			{
				xmlFile=path + "config/export_equi.xml";
				list=new ArrayList<Rtu>();
				list = equiService.selectCommon(record, null,user.getUcode());
			}else if(flag != null && flag.trim().equals("3")){
				xmlFile=path + "config/export_equi.xml";
				list=new ArrayList<Rtu>();
				list = equiService.selectBoXing(record, null,user.getUcode());
			}else if(flag != null && flag.trim().equals("zk")){
				xmlFile=path + "config/export_zk_rtu.xml";
				list=new ArrayList<ZKRtu>();
				list = equiService.selectZKRtu(zkrtu, null, user.getUcode());
			}else if(flag != null && flag.trim().equals("wtld")){
				xmlFile=path + "config/export_wtld_rtu.xml";
				list=new ArrayList<WtldRtu>();
				list = equiService.selectWtldRtu(wtld, null, user.getUcode());
			}else if(flag != null && flag.trim().equals("wtldszy")){
				xmlFile=path + "config/export_wtldszy_rtu.xml";
				list=new ArrayList<WtldSzyRtu>();
				list = equiService.selectWtldSzyRtu(wtldszy, null, user.getUcode());
			}
			
			
			XmlConfig config = null;
			ParseXmlFile parse = new ParseXmlFile();
			config = parse.parseXml(xmlFile);
			CreateExcelPoi poi = new CreateExcelPoi();
			poi.setFileName("sheet1");
			String[] columnName = config.getColumnName();
			String[] filedName = config.getFieldName();		
			int[] refindex = config.getRefindex();
			String[] refcon = config.getRefcon();
			String[] type = config.getCellType();
			String replace = "#airpressurecal:false:否;true:是;#";//"#sex:0,1;1,2;#";
			poi.setColumnName(columnName);
			poi.setFiledName(filedName);
			poi.setRefcon(refcon);
			poi.setRefindex(refindex);
			poi.setReplace(replace);	
			poi.setCellType(type);
			poi.setFileSavePath(path+"temp/");		
			poi.setList(list);
			poi.setCls(Rtu.class);		
					
			String name = "监测设备信息表";			
			response.setHeader("Accept-Ranges", "bytes");
			response.setHeader("Content-disposition", "attachment; filename=" + new String(name.getBytes("GBK"), "ISO8859-1") + ".xls");
	        response.setContentType("application/octet-stream");
//	        response.setHeader("Content-Length", String.valueOf(file.length()));
//			response.setHeader("Content-Disposition", "attachment; filename=data_" + record.getStationnumber()+".xls");
			InputStream stream = poi.getExcelStream(true);
			OutputStream out = response.getOutputStream();
			byte[] data = new byte[1024];
			int len = 0;
			while((len = stream.read(data)) !=-1)
			{
				out.write(data,0,len);
			}
			stream.close();
			out.flush();
			out.close();
			}
			catch(Exception e)
			{
				e.printStackTrace();
				
			}
	}
}
